import utils from './../utils'
import * as types from '../constants/user'
import store from 'store'


//获取token
export function getToken(){
  let Authorization=localStorage.getItem('Authorization');
  if(Authorization) return 
  return utils.get(utils.url.getToken)
  .then(res=>{
    localStorage.setItem('Authorization', res.data);
  })
  
}
//退出登陆
export function loginOut(){
  localStorage.removeItem('Authorization');
  localStorage.removeItem('userName');
  localStorage.removeItem('userId');
}

//登陆时，获取图片验证码
export const getLoginImgCode =(data,config={})=>{
  data= {...data,...{func:'get-captcha'}}
    return (dispatch,getState)=>{
	return utils.form(utils.url.index,data)
	.then(res=>{
		return dispatch({type:types.GET_CAPTCHA,data:res.data})
	})
	}
}

//登陆 and 注册
export const login =(data,config={callback:()=>{}})=>{
    return (dispatch,getState)=>{
  return utils.post(utils.url.index,data)
  .then(res=>{
      config.callback(res)
      if(res.code ==200){
        return dispatch({type:types.CLEAR_CAPTCHA})
      }
      
    
    
  })
  }
}

//获取用户积分信息
export const getScoreInfo =(data,config={})=>{
  data= {...data,...{func:'score-info'}}
    return (dispatch,getState)=>{
  return utils.form(utils.url.index,data)
  .then(res=>{
    config.callback && config.callback(res)
  })
  }
}

//通过用户ID读取用户信息
export const getUserInfo =(data,config={})=>{
  data= {...data,...{func:'get-user-info'}}
    return (dispatch,getState)=>{
  return utils.form(utils.url.index,data)
  .then(res=>{
    config.callback && config.callback(res)
  })
  }
}

//更新用户信息资料
export const updateUserInfo =(data,config={})=>{
  data= {...data,...{func:'update-user-info'}}
    return (dispatch,getState)=>{
  return utils.form(utils.url.index,data)
  .then(res=>{
    config.callback && config.callback(res)
  })
  }
}




































